#include "SettingsDefinitions.h"

IntSetting *pose_pid_p;
IntSetting *pose_pid_i;
IntSetting *pose_pid_d;

IntSetting *one_full_step_delay_ms;
IntSetting *motor_full_steps_per_rev;
IntSetting *half_current_time_ms;

IntSetting *closed_loop_mode_current_max_ma;
IntSetting *open_loop_mode_current_max_ma;
IntSetting *calibration_mode_current_max_ma;
IntSetting *lowpass_filtering_a;
IntSetting *lowpass_filtering_b;

void make_settings()
{
    // init
    Setting::init();

    // new
    pose_pid_p = new IntSetting(MC, WG, "0", "pose pid p", DEFAULT_POSE_PID_P, 0, 1000);
    pose_pid_i = new IntSetting(MC, WG, "1", "pose pid i", DEFAULT_POSE_PID_I, 0, 1000);
    pose_pid_d = new IntSetting(MC, WG, "2", "pose pid d", DEFAULT_POSE_PID_D, 0, 1000);

    one_full_step_delay_ms = new IntSetting(MC, WG, "3", "one full step delay/ms", DEFAULT_ONE_FULL_STEP_DELAY_MS, 0, 99999);
    motor_full_steps_per_rev = new IntSetting(MC, WG, "4", "motor full steps/rev", DEFAULT_MOTOR_FULL_STEPS_PER_REV, 0, 99999);
    half_current_time_ms = new IntSetting(MC, WG, "5", "half current time/ms", DEFAULT_HALF_CURRENT_MAX_MS, 0, 99999);

    closed_loop_mode_current_max_ma = new IntSetting(MC, WG, "6", "max closed loop mode current/mA", DEFAULT_CLOSED_LOOP_MODE_CURRENT_MAX_MA, 0, 3300);
    open_loop_mode_current_max_ma = new IntSetting(MC, WG, "7", "max open loop mode current/mA", DEFAULT_OPEN_LOOP_MODE_CURRENT_MAX_MA, 0, 3300);
    calibration_mode_current_max_ma = new IntSetting(MC, WG, "", "calibration mode current/mA", DEFAULT_CALIBRATION_MODE_CURRENT_MAX_MA, 0, 3300);
    lowpass_filtering_a = new IntSetting(MC, WG, "8", "lowpass filtering a", DEFAULT_LOWPASS_FILTERING_A, 0, 0);
    lowpass_filtering_b = new IntSetting(MC, WG, "9", "lowpass filtering b", DEFAULT_LOWPASS_FILTERING_B, 0, 0);

    // load
    for (Setting *s = Setting::List; s; s = s->next())
    {
        s->load();
    }
}

Error do_command_or_setting(const char *key, char *value, char *out)
{
    // If value is "", it means that there was no value string, i.e.
    // $key without =, or [key] with nothing following.
    // If value is not "", but the string is empty, that is the form
    // $key= with nothing following the = .  It is important to distinguish
    // those cases so that you can say "$N0=" to clear a startup line.

    // First search the settings list by text name.  If found, set a new
    // value if one is given, otherwise display the current value
    for (Setting *s = Setting::List; s; s = s->next())
    {
        //DEBUG_port->printf("getName:%s, key:%s\r\n", s->getName(), key);
        if (strcasecmp(s->getName(), key) == 0)
        {
            if (value)
            {
                //DEBUG_port->printf("+++++\r\n");
                delay(10);
                return s->setStringValue(value);
            }
            else
            {
                //DEBUG_port->printf("-----\r\n");
                sprintf(out, "$%s=%s", s->getName(), s->getStringValue());
                //DEBUG_port->printf("$%s=%s", s->getName(), s->getStringValue());
                return Error::Ok;
            }
        }
    }

    for (Setting *s = Setting::List; s; s = s->next())
    {
        //DEBUG_port->printf("getGrblName:%s, key:%s\r\n", s->getGrblName(), key);
        if (s->getGrblName() && strcasecmp(s->getGrblName(), key) == 0)
        {
            if (value)
            {
                return s->setStringValue(value);
            }
            else
            {
                sprintf(out, "$%s=%s", s->getName(), s->getStringValue());
                //DEBUG_port->printf("$%s=%s", s->getName(), s->getStringValue());
                return Error::Ok;
            }
        }
    }

    return Error::InvalidStatement;
}
